﻿using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using Sumc.Client.ModernUI.Services;
using System;

namespace Sumc.Client.ModernUI.ViewModels
{
    public class CaptchaFlyoutViewModel : BindableBase, IFlyoutViewModel
    {
        private string token;
        private Action closeFlyout;
        private Action goBack;
        private Action successAction;
        private DelegateCommand postCaptcha;
        private DelegateCommand goBackcommand;
        private string code;
        private string captchaUrl;
        private bool loadingData = true;
        private bool isCaptchaIncorrectlyEntered;
        private readonly ISessionRepository sessionRepository;

        public CaptchaFlyoutViewModel(ISessionRepository sessionRepository)
        {
            this.sessionRepository = sessionRepository;
        }

        public DelegateCommand GoBackCommand
        {
            get
            {
                if (this.goBackcommand == null)
                {
                    this.goBackcommand = new DelegateCommand(() => this.CloseFlyout());
                }

                return this.goBackcommand;
            }
        }

        public DelegateCommand PostCaptcha
        {
            get
            {
                if (this.postCaptcha == null)
                {
                    this.postCaptcha = new DelegateCommand(this.PostCaptchaHandler);
                }

                return this.postCaptcha;
            }
        }

        public bool IsCaptchaIncorrectlyEntered
        {
            get
            {
                return this.isCaptchaIncorrectlyEntered;
            }

            set
            {
                this.SetProperty(ref this.isCaptchaIncorrectlyEntered, value);
            }
        }

        public bool LoadingData
        {
            get
            {
                return this.loadingData;
            }

            set
            {
                this.SetProperty(ref this.loadingData, value);
            }
        }

        public string CaptchaUrl
        {
            get
            {
                return this.captchaUrl;
            }

            set
            {
                this.SetProperty(ref this.captchaUrl, value);
            }
        }

        public string Code
        {
            get
            {
                return this.code;
            }

            set
            {
                this.SetProperty(ref this.code, value);
            }
        }

        public Action CloseFlyout
        {
            get
            {
                return this.closeFlyout;
            }

            set
            {
                this.SetProperty(ref this.closeFlyout, value);
            }
        }

        public Action GoBack
        {
            get
            {
                return this.goBack;
            }

            set
            {
                SetProperty(ref this.goBack, value);
            }
        }

        public void Open(object parameter, Action successAction)
        {
            this.successAction = successAction;
            this.LoadData();
        }

        private async void LoadData()
        {
            try
            {
                this.LoadingData = true;
                var authForm = await this.sessionRepository.GetCaptchaCode();
                this.CaptchaUrl = authForm.CaptchaUrl;
                this.token = authForm.Token;
            }
            catch (Exception)
            {
                //Token is already resolved.
                this.CloseFlyoutWidthSuccess();
            }
            finally
            {
                this.LoadingData = false;
            }
        }

        private async void PostCaptchaHandler()
        {
            try
            {
                this.LoadingData = true;
                var isSuccessfully = await this.sessionRepository.PostCaptchaCode(this.token, this.Code);
                this.LoadingData = false;
                if (isSuccessfully)
                {
                    this.CloseFlyoutWidthSuccess();
                }
                else
                {
                    //Wrong code
                    this.IsCaptchaIncorrectlyEntered = true;
                    this.LoadData();
                }
            }
            catch (Exception)
            {
                //Token is already resolved by other user.
                this.CloseFlyoutWidthSuccess();
            }
            finally
            {
                this.LoadingData = false;
            }
        }

        private void CloseFlyoutWidthSuccess()
        {
            if (this.successAction != null)
            {
                this.successAction();
            }

            if (this.CloseFlyout != null)
            {
                this.CloseFlyout();
            }
        }
    }
}